"""
字幕服务层
提供字幕处理相关的业务逻辑
"""

import os
import logging
from typing import Dict, Any
from app.tools.subtitle_tools import SubtitleTools
from app.tools.utils import get_file_size_mb

class SubtitleService:
    """字幕服务类"""
    
    def __init__(self, upload_folder: str, output_folder: str):
        """
        初始化字幕服务
        
        Args:
            upload_folder: 上传文件夹路径
            output_folder: 输出文件夹路径
        """
        self.upload_folder = upload_folder
        self.output_folder = output_folder
        self.subtitle_tools = SubtitleTools()
        
    def add_subtitle_service(self, video_path: str, subtitle_path: str) -> Dict[str, Any]:
        """
        添加字幕服务
        
        Args:
            video_path: 视频文件路径
            subtitle_path: 字幕文件路径
            
        Returns:
            处理结果字典
        """
        try:
            # 获取原始文件信息
            video_size = get_file_size_mb(video_path)
            
            # 添加字幕
            output_path = self.subtitle_tools.add_subtitle(video_path, subtitle_path, self.output_folder)
            
            # 获取输出文件信息
            output_size = get_file_size_mb(output_path)
            
            return {
                'success': True,
                'output_path': output_path,
                'original_size_mb': video_size,
                'output_size_mb': output_size,
                'message': '字幕添加成功'
            }
        except Exception as e:
            logging.error(f"字幕添加失败: {e}")
            return {
                'success': False,
                'error': str(e),
                'message': '字幕添加失败'
            } 

    def apply_subtitle_service(self, video_path: str, subtitle_path: str, subtitle_settings: dict) -> Dict[str, Any]:
        """
        应用字幕设置到视频
        
        Args:
            video_path: 视频文件路径
            subtitle_path: 字幕文件路径
            subtitle_settings: 字幕设置信息
            
        Returns:
            处理结果字典
        """
        try:
            # 获取原始文件信息
            video_size = get_file_size_mb(video_path)
            
            # 应用字幕设置
            output_path = self.subtitle_tools.apply_subtitle_with_settings(video_path, subtitle_path, subtitle_settings, self.output_folder)
            
            # 获取输出文件信息
            output_size = get_file_size_mb(output_path)
            
            return {
                'success': True,
                'output_path': output_path,
                'original_size_mb': video_size,
                'output_size_mb': output_size,
                'message': '字幕应用成功'
            }
        except Exception as e:
            logging.error(f"字幕应用失败: {e}")
            return {
                'success': False,
                'error': str(e),
                'message': '字幕应用失败'
            } 